package com.htandroid.htandroid.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.AbToastUtil;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.aty.Cggl_qg_index;
import com.htandroid.htandroid.aty.Cggl_sh_index;
import com.htandroid.htandroid.aty.Cggl_zb_index;
import com.htandroid.htandroid.aty.Gcwx_check_index;
import com.htandroid.htandroid.aty.Gcwx_jd_index;
import com.htandroid.htandroid.aty.Gcwx_lr_index;
import com.htandroid.htandroid.aty.Gcwx_xd_index;
import com.htandroid.htandroid.aty.jtsp_index;
import com.htandroid.htandroid.aty.kcapp_index;
import com.htandroid.htandroid.aty.mrqgd_index;
import com.htandroid.htandroid.aty.mrqgdsp_index;
import com.htandroid.htandroid.aty.qgddj_index;
import com.htandroid.htandroid.aty.qgdsp_index;
import com.htandroid.htandroid.aty.zcpd_index;
import com.htandroid.htandroid.aty.zcview_detail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caih on 2018/8/22.
 */
public class MainItemGridAdapter extends BaseAdapter {

    private List<Integer> childs;
    private List<Integer> childList = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;

    public MainItemGridAdapter(Context context, List<Integer> childs) {
        this.context = context;
        this.childs = childs;
        inflater = LayoutInflater.from(context);
        for (int i = 0; i < childs.size(); i++) {
            childList.add(childs.get(i));
        }
    }

    public void scale(boolean scale) {
        childList.clear();
        for (int i = 0; i < (childs.size() > 4 && scale ? 4 : childs.size()); i++) {
            childList.add(childs.get(i));
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return childList.size();
    }

    @Override
    public Object getItem(int position) {
        return childList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.layout_item_b, null);
            holder.layout = convertView.findViewById(R.id.ll_main_item);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_main_item);
            holder.textView = (TextView) convertView.findViewById(R.id.tv_main_item);
            holder.view = convertView.findViewById(R.id.v_main_item);
            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();
        int child = childList.get(position);
        holder.imageView.setImageResource(getImgRes(child));
        holder.textView.setText(getStr(child));
        holder.view.setVisibility(childList.size() > 4 ? View.VISIBLE : View.GONE);
        holder.layout.setTag(child);
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTo((Integer) v.getTag());
            }
        });
        return convertView;
    }

    public class ViewHolder {
        public View layout;
        public ImageView imageView;
        public TextView textView;
        public View view;
    }

    private int getImgRes(int child) {
        int res = R.mipmap.main_1;
        switch (child) {
            case 1:
                res = R.mipmap.main_1;
                break;
            case 2:
                res = R.mipmap.main_2;
                break;
            case 3:
                res = R.mipmap.main_3;
                break;
            case 4:
                res = R.mipmap.main_4;
                break;
            case 5:
                res = R.mipmap.main_5;
                break;
            case 6:
                res = R.mipmap.main_6;
                break;
            case 7:
                res = R.mipmap.main_7;
                break;
            case 8:
                res = R.mipmap.main_8;
                break;
            case 9:
                res = R.mipmap.main_9;
                break;
            case 10:
                res = R.mipmap.main_10;
                break;
            case 11:
                res = R.mipmap.main_11;
                break;
            case 12:
                res = R.mipmap.main_12;
                break;
            case 13:
                res = R.mipmap.main_11;
                break;
            case 14:
                res = R.mipmap.main_12;
                break;
            case 15:
                res = R.mipmap.main_13;
                break;
            case 16:
                res = R.mipmap.main_14;
                break;
            case 17:
                res = R.mipmap.main_15;
                break;
            case 18:
                res = R.mipmap.main_16;
                break;
            case 19:
                res = R.mipmap.main_16;
                break;
            case 20:
                res = R.mipmap.main_17;
                break;
        }
        return res;
    }

    private String getStr(int child) {
        String str = "";
        switch (child) {
            case 1:
                str = "部门请购定价";
                break;
            case 2:
                str = "部门请购审批";
                break;
            case 3:
                str = "厨房请购审批";
                break;
            case 4:
                str = "厨房请购清单";
                break;
            case 5:
                str = "请购生成订单";
                break;
            case 6:
                str = "订单生成收货";
                break;
            case 7:
                str = "订单生成直拨";
                break;
            case 8:
                str = "库存申请审批";
                break;
            case 9:
                str = "付款单审批";
                break;
            case 10:
                str = "集团请购审批";
                break;
            case 11:
                str = "资产盘点";
                break;
            case 12:
                str = "资产查看";
                break;
            case 13:
                str = "低易品盘点";
                break;
            case 14:
                str = "低易品查看";
                break;
            case 15:
                str = "工程报修";
                break;
            case 16:
                str = "维修接单";
                break;
            case 17:
                str = "维修销单";
                break;
            case 18:
                str = "维修审核";
                break;
            case 19:
                str = "维修清单";
                break;
            case 20:
                str = "工作日报表";
                break;
        }
        return str;
    }

    private void startTo(int child) {
        switch (child) {
            case 1:
                context.startActivity(new Intent(context, qgddj_index.class));
                break;
            case 2:
                context.startActivity(new Intent(context, qgdsp_index.class));
                break;
            case 3:
                context.startActivity(new Intent(context, mrqgdsp_index.class));
                break;
            case 4:
                context.startActivity(new Intent(context, mrqgd_index.class));
                break;
            case 5:
                context.startActivity(new Intent(context, Cggl_qg_index.class));
                break;
            case 6:
                context.startActivity(new Intent(context, Cggl_sh_index.class));
                break;
            case 7:
                context.startActivity(new Intent(context, Cggl_zb_index.class));
                break;
            case 8:
                context.startActivity(new Intent(context, kcapp_index.class));
                break;
            case 9:
                AbToastUtil.showToast(context, "敬请期待！");
                break;
            case 10:
                context.startActivity(new Intent(context, jtsp_index.class));
                break;
            case 11:
                context.startActivity(new Intent(context, zcpd_index.class));
                break;
            case 12:
                context.startActivity(new Intent(context, zcview_detail.class));
                break;
            case 13:
                AbToastUtil.showToast(context, "敬请期待！");
                break;
            case 14:
                AbToastUtil.showToast(context, "敬请期待！");
                break;
            case 15:
                context.startActivity(new Intent(context, Gcwx_lr_index.class));
                break;
            case 16:
                context.startActivity(new Intent(context, Gcwx_jd_index.class));
                break;
            case 17:
                context.startActivity(new Intent(context, Gcwx_xd_index.class));
                break;
            case 18:
                context.startActivity(new Intent(context, Gcwx_check_index.class));
                break;
            case 19:
                AbToastUtil.showToast(context, "敬请期待！");
                break;
            case 20:
                AbToastUtil.showToast(context, "敬请期待！");
                break;
        }
    }

}
